﻿/****************************************************************************************
 *
 *  Filename:       Form1.cs
 *  Description:    Simple test form for LogSubtPol
 *  License: GPLv2: http://logsubstpol.codeplex.com/license
 *
 *  History
 *  -------
 *
 *    Date   | Author          | Comment
 *  ---------+-----------------+---------------------------------------------------------
 *  08.08.09 | jkalmbach_cp    | First version published on
 *           |                 | Codeplex: http://logsubstpol.codeplex.com/
 *  $log$
 *
 ****************************************************************************************
 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.TeamFoundation.VersionControl.Client;

namespace TestApp
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
      p.FillCommentInSourceFile(this.textBox1.Text, "c:\\test.cpp", null);
    }


    LogSubstPol.LogSubstPol p = new LogSubstPol.LogSubstPol();
    private void button2_Click(object sender, EventArgs e)
    {
      InitTeamproject();
      p.Edit(new PEA() { TeamProject = tp, Parent = this });
    }

    private void button3_Click(object sender, EventArgs e)
    {
      InitTeamproject();
      polSet = p.Load(tp, polSet);
    }

    LogSubstPol.LogSubstPolSetting polSet;
    TeamProject tp;

    private void button4_Click(object sender, EventArgs e)
    {
      InitTeamproject();
      p.Save(tp);
    }

    void InitTeamproject()
    {
      if (tp == null)
      {
        var tpp = new Microsoft.TeamFoundation.Client.TeamProjectPicker(Microsoft.TeamFoundation.Client.TeamProjectPickerMode.SingleProject, true);
        if (tpp.ShowDialog(this) == DialogResult.OK)
        {
          var tpi = tpp.SelectedProjects[0];
          var vcs = tpp.SelectedTeamProjectCollection.GetService<VersionControlServer>();
          tp = vcs.GetTeamProject(tpi.Name);
        }
      }
    }

    class PEA : IPolicyEditArgs
    {
      public bool IsNew
      {
        get { throw new NotImplementedException(); }
      }

      public IWin32Window Parent { get; set; }

      public TeamProject TeamProject { get; set; }

      public object GetService(Type serviceType)
      {
        throw new NotImplementedException();
      }
    }

    private void button5_Click(object sender, EventArgs e)
    {
      InitTeamproject();
      p.Initialize(new PCI()
      {
        TFS = tp.TeamProjectCollection,
        PendingChanges = new PCPC()
        {
          AllPendingChanges = tp.VersionControlServer.GetWorkspace(Environment.MachineName, tp.VersionControlServer.AuthorizedUser).GetPendingChanges(),
          Comment = this.textBox1.Text
        }
      });
      p.Evaluate();
    }

    /// <summary>
    /// My Dummy-Interface, to test the check-in policy
    /// </summary>
    class PCPC : IPendingCheckinPendingChanges
    {
      public string[] AffectedTeamProjectPaths
      {
        get { throw new NotImplementedException(); }
      }

      public event AffectedTeamProjectsEventHandler AffectedTeamProjectsChanged;
      private void RaiseAffectedTeamProjectsChanged()
      {
        if (AffectedTeamProjectsChanged != null)
          AffectedTeamProjectsChanged(null, null);
      }

      PendingChange[] _AllPendingChanges;
      public PendingChange[] AllPendingChanges
      {
        get { return _AllPendingChanges; }
        set { _AllPendingChanges = value; }
      }

      public PendingChange[] CheckedPendingChanges
      {
        get
        {
          return _AllPendingChanges;
        }
        set { }
      }

      public event EventHandler CheckedPendingChangesChanged;
      private void RaiseCheckedPendingChangesChanged()
      {
        if (CheckedPendingChangesChanged != null)
          CheckedPendingChangesChanged(null, null);
      }

      public string Comment { get; set; }

      #region IPendingCheckinPendingChanges Members


      public Workspace Workspace
      {
        get { throw new NotImplementedException(); }
      }

      #endregion
    }
    class PCI : IPendingCheckin
    {
      public IPendingCheckinPendingChanges PendingChanges { get; set; }

      public Microsoft.TeamFoundation.Client.TfsTeamProjectCollection TFS;
      public object GetService(Type serviceType)
      {
        return TFS.GetService(serviceType);
      }


      #region IPendingCheckin Members

      public IPendingCheckinNotes CheckinNotes
      {
        get { throw new NotImplementedException(); }
      }

      public IPendingCheckinPolicies Policies
      {
        get { throw new NotImplementedException(); }
      }

      public IPendingCheckinWorkItems WorkItems
      {
        get { throw new NotImplementedException(); }
      }

      #endregion
    }
  }
}
